using System;
using System.Collections.Generic;
using System.Text;

namespace GetTogether.Studio
{
    public class ProjectParameter
    {
        public enum ProjectCategory
        {
            WebService,
            Logging,
            Database,
            Schedule
        }

        #region Functions

        public static string GetSettingsPathPublic(ProjectCategory prjCategory)
        {
            return System.IO.Path.Combine(Setting.GetIncludeFolder(), string.Concat(prjCategory.ToString(), "\\Projects\\"));
        }

        public static string GetSettingsPath(ProjectCategory prjCategory, string username)
        {
            string directory = "";
            if (!string.IsNullOrEmpty(username))
                directory = string.Concat(GetSettingsPathPublic(prjCategory), username, "\\");
            else
                directory = GetSettingsPathPublic(prjCategory);
            if (!System.IO.Directory.Exists(directory)) System.IO.Directory.CreateDirectory(directory);
            return directory;
        }

        public static T GetSettingsByProjectName<T>(ProjectCategory prjCategory, string username, string projName, string shareUsername)
            where T : class, new()
        {
            T s = null;
            string file = "";
            if (!string.IsNullOrEmpty(username)) file = string.Concat(GetSettingsPath(prjCategory, username), projName);
            else file = string.Concat(GetSettingsPathPublic(prjCategory), projName);

            if (!string.IsNullOrEmpty(shareUsername) && !System.IO.File.Exists(file))
            {
                string shareUserPrjFile = string.Concat(GetSettingsPath(prjCategory, shareUsername), projName);
                if (System.IO.File.Exists(shareUserPrjFile))
                {
                    System.IO.File.Copy(shareUserPrjFile, file);
                }
            }
            if (!System.IO.File.Exists(file))
            {
                return null;
            }
            s = GetTogether.Utility.SerializationHelper.FromXml<T>(System.IO.File.ReadAllText(file));
            return s;
        }

        #endregion
    }
}
